package com.yeyouluo.bigdata.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class HomeWorkDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        // get job configuration
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);

        // set class driver
        job.setJarByClass(HomeWorkDriver.class);

        // 使用CombineTextInputFormat读取多个小文件
        job.setInputFormatClass(CombineTextInputFormat.class);
        CombineTextInputFormat.setMaxInputSplitSize(job, 4194304);

        // set class mapper & reducer
        job.setMapperClass(HomeworkMapper.class);
        job.setReducerClass(HomeworkReducer.class);

        // set map output
        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(IntWritable.class);

        // set reduce output
        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(IntWritable.class);

        // parse args: input & output path
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        // submit & exit
        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);
    }
}
